package edu.chinasoft.exception;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DispatcherServletException {


    //@ExceptionHandler
    //public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    //    ModelAndView modelAndView = new ModelAndView();
    //
    //    if(ex instanceof MapperException){
    //        modelAndView.addObject("error","mapper层发生异常！");
    //        modelAndView.addObject("ex",ex);
    //
    //        modelAndView.setViewName("/error/mapper");
    //    }else if(ex instanceof ServiceException){
    //        modelAndView.addObject("error","服务器层发生异常！");
    //        modelAndView.addObject("ex",ex);
    //
    //        modelAndView.setViewName("/error/service");
    //    }else if(ex instanceof  HandlerException){
    //        modelAndView.addObject("error","处理层发生异常！");
    //        modelAndView.addObject("ex",ex);
    //
    //        modelAndView.setViewName("/error/handler");
    //    }else{
    //        modelAndView.addObject("error","中央控制器发生异常！");
    //        modelAndView.addObject("ex",ex);
    //
    //        modelAndView.setViewName("/error/other");
    //    }
    //
    //
    //    return modelAndView;
    //}


    @ExceptionHandler(MapperException.class)
    public ModelAndView resolveExceptionForMapperException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("error", "mapper层发生异常！");
        modelAndView.addObject("ex", ex);

        modelAndView.setViewName("/error/mapper");

        return modelAndView;
    }

    @ExceptionHandler(ServiceException.class)
    public ModelAndView resolveExceptionForServiceException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("error", "服务器层发生异常！");
        modelAndView.addObject("ex", ex);

        modelAndView.setViewName("/error/mapper");

        return modelAndView;
    }

    @ExceptionHandler(HandlerException.class)
    public ModelAndView resolveExceptionForHandlerException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("error", "处理层发生异常！");
        modelAndView.addObject("ex", ex);

        modelAndView.setViewName("/error/mapper");

        return modelAndView;
    }



}
